package rabbitmq.simple;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ProducerDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.建立连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("1.92.79.64");
        connectionFactory.setPort(5672); //需提前开放端口号
        connectionFactory.setUsername("admin");//账号
        connectionFactory.setPassword("admin");//密码
        connectionFactory.setVirtualHost("study");//虚拟主机
        Connection connection = connectionFactory.newConnection();
        //2.开启信道
        Channel channel = connection.createChannel();
        //3.声明交换机 使用内置的交换机
        //4.声明队列
        /**
         * Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
         *                                  Map<String, Object> arguments) throws IOException;
         *
         * 参数说明：
         *  queue：队列名称
         *  durable：可持久化
         *  exclusive：是否独占
         *  autoDelete：是否自动删除
         *  arguments：额外参数
         */
        channel.queueDeclare("hello",true,false,false,null);
        //5.发消息
        /**
         * void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;
         *
         * 参数声明：
         *   exchange：交换机名称，不写代表使用内置交换机
         *   routingKey：路由名称, routingKey = 队列名称 （使用内置交换机，routingKey与队列名称保持一致）
         *   props：属性配置
         *   body：消息
         */
        for (int i = 0; i < 10; i++) {
            String msg = "hello rabbitmq ~" + i;
            channel.basicPublish("","hello",null,msg.getBytes());
        }
        System.out.println("消息发送成功~");


        //6.资源释放
/*        channel.close();
        connection.close();*/
    }
}
